package com.barbeque.mall.service;

import com.barbeque.mall.bean.config.*;
import com.barbeque.mall.mapper.MarketSystemMapper;
import com.barbeque.mall.util.ConfigServiceReflectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.Field;
import java.util.List;

/**
 * @author Tezel
 * @since 2022/07/18 10:35
 */

@Service
public class ConfigServiceImpl implements ConfigService {

    @Autowired
    MarketSystemMapper systemMapper;

    @Override
    public MallConfigVo mall() {
        MarketSystemExample systemExample = new MarketSystemExample();
        MarketSystemExample.Criteria criteria = systemExample.createCriteria();
        criteria.andKeyNameLike("market_mall"+"%");

        List<MarketSystem> marketSystems = systemMapper.selectByExample(systemExample);

        MallConfigVo mallConfigVo = new MallConfigVo();

        for (MarketSystem marketSystem : marketSystems) {
            if(marketSystem.getKeyName().contains("address")){
                mallConfigVo.setMarket_mall_address(marketSystem.getKeyValue());
            }
            if(marketSystem.getKeyName().contains("latitude")){
                mallConfigVo.setMarket_mall_latitude(marketSystem.getKeyValue());
            }
            if(marketSystem.getKeyName().contains("longitude")){
                mallConfigVo.setMarket_mall_longitude(marketSystem.getKeyValue());
            }
            if(marketSystem.getKeyName().contains("name")){
                mallConfigVo.setMarket_mall_name(marketSystem.getKeyValue());
            }
            if(marketSystem.getKeyName().contains("phone")){
                mallConfigVo.setMarket_mall_phone(marketSystem.getKeyValue());
            }
            if(marketSystem.getKeyName().contains("qq")){
                mallConfigVo.setMarket_mall_qq(marketSystem.getKeyValue());
            }

        }
        return mallConfigVo;

    }

    @Override
    public void updateMall(MallConfigVo mallConfig) {
        Class<? extends MallConfigVo> aClass = mallConfig.getClass();
        Field[] declaredFields = aClass.getDeclaredFields();
        for (Field declaredField : declaredFields) {

            declaredField.setAccessible(true);

            MarketSystem marketSystem = new MarketSystem();

            marketSystem.setKeyName(declaredField.getName());

            try {
                marketSystem.setKeyValue((String) declaredField.get(mallConfig));
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }

            MarketSystemExample marketSystemExample = new MarketSystemExample();
            MarketSystemExample.Criteria criteria = marketSystemExample.createCriteria();
            criteria.andKeyNameLike(marketSystem.getKeyName());

            systemMapper.updateByExampleSelective(marketSystem,marketSystemExample);

        }
    }

    /**
     * 显示express数据
     * @author Tezel
     * @date 2022/07/18
     */


    @Override
    public ExpressConfigVo express() {
        MarketSystemExample systemExample = new MarketSystemExample();
        MarketSystemExample.Criteria criteria = systemExample.createCriteria();
        criteria.andKeyNameLike("market_express"+"%");

        List<MarketSystem> marketSystems = systemMapper.selectByExample(systemExample);

        ExpressConfigVo expressConfigVo = new ExpressConfigVo();


        for (MarketSystem marketSystem : marketSystems) {
            if(marketSystem.getKeyName().contains("freight_min")){
                expressConfigVo.setMarket_express_freight_min(marketSystem.getKeyValue());
            }
            if(marketSystem.getKeyName().contains("freight_value")){
                expressConfigVo.setMarket_express_freight_value(marketSystem.getKeyValue());
            }

        }

        return expressConfigVo;
    }

    /**
     * 更改express信息
     * @author Tezel
     * @date 2022/07/18
     */

    @Override
    public void updateExpress(ExpressConfigVo expressConfigVo) {

        //工具类封装

        ConfigServiceReflectionUtils.updateConfig(expressConfigVo, systemMapper);

    }



    @Override
    public OrderConfigVo order() {
        MarketSystemExample systemExample = new MarketSystemExample();
        MarketSystemExample.Criteria criteria = systemExample.createCriteria();
        criteria.andKeyNameLike("market_order"+"%");

        List<MarketSystem> marketSystems = systemMapper.selectByExample(systemExample);

        OrderConfigVo orderConfigVo = new OrderConfigVo();


        for (MarketSystem marketSystem : marketSystems) {
            if(marketSystem.getKeyName().contains("unconfirm")){
                orderConfigVo.setMarket_order_unconfirm(marketSystem.getKeyValue());
            }
            if(marketSystem.getKeyName().contains("unpaid")){
                orderConfigVo.setMarket_order_unpaid(marketSystem.getKeyValue());
            }
            if(marketSystem.getKeyName().contains("comment")){
                orderConfigVo.setMarket_order_comment(marketSystem.getKeyValue());
            }

        }

        return orderConfigVo;
    }


    @Override
    public void updateOrder(OrderConfigVo orderConfigVo) {
        ConfigServiceReflectionUtils.updateConfig(orderConfigVo, systemMapper);
    }


    @Override
    public WxConfigVo wx() {
        MarketSystemExample systemExample = new MarketSystemExample();
        MarketSystemExample.Criteria criteria = systemExample.createCriteria();
        criteria.andKeyNameLike("market_wx"+"%");

        List<MarketSystem> marketSystems = systemMapper.selectByExample(systemExample);

        WxConfigVo wxConfigVo = new WxConfigVo();


        for (MarketSystem marketSystem : marketSystems) {
            if(marketSystem.getKeyName().contains("goods")){
                wxConfigVo.setMarket_wx_catlog_goods(marketSystem.getKeyValue());
            }
            if(marketSystem.getKeyName().contains("list")){
                wxConfigVo.setMarket_wx_catlog_list(marketSystem.getKeyValue());
            }
            if(marketSystem.getKeyName().contains("brand")){
                wxConfigVo.setMarket_wx_index_brand(marketSystem.getKeyValue());
            }
            if(marketSystem.getKeyName().contains("hot")){
                wxConfigVo.setMarket_wx_index_hot(marketSystem.getKeyValue());
            }
            if(marketSystem.getKeyName().contains("new")){
                wxConfigVo.setMarket_wx_index_new(marketSystem.getKeyValue());
            }
            if(marketSystem.getKeyName().contains("topic")){
                wxConfigVo.setMarket_wx_index_topic(marketSystem.getKeyValue());
            }
            if(marketSystem.getKeyName().contains("share")){
                wxConfigVo.setMarket_wx_share(marketSystem.getKeyValue());
            }

        }

        return wxConfigVo;
    }

    /**
     * 更新小程序配置信息
     * @author Tezel
     * @date 2022/07/18
     */
    @Override
    public void updateWx(WxConfigVo wxConfigVo) {

        ConfigServiceReflectionUtils.updateConfig(wxConfigVo, systemMapper);

    }
}
